const {
	Service
} = require('dx-router-admin')

module.exports = class Resource extends Service {
	constructor(ctx) {
		super(ctx)
		this.table = 'opendb-resource';
		this.create_time = 'create_date';
		this.fields = {
			create_date: '创建时间',
			user_id: '用户ID',
			file_size: "文件大小",
			type: "文件类型",
			file_name: "文件名称",
			url: "文件url",
			fileID: "文件ID"
		}
		this.init();
	}
	//初始化
	init() {
		this.beforeInsert(async (that) => {
			const payload = await this.$dx.uniID.checkToken(this.$dx.request.uniIdToken());			
			if (payload.code) {
				this.$dx.throw(payload);
			}
			that.user_id = payload.uid;
			return true;
		})
		//编辑的时候
		this.beforeUpdate(async (that) => {
			return true;
		})

		this.afterInsert(function() {
			console.log('新增成功')
		})
		//删除，去删除云文件
		this.afterDelete(async function(that) {
			await uniCloud.deleteFile({
				fileList: [that.fileID]
			});
		})
	}

}
